

package com.hazelcast.internal.metrics.metricsets;

import com.hazelcast.internal.metrics.MetricsRegistry;
import com.hazelcast.internal.util.JVMUtil;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;

import static com.hazelcast.internal.metrics.MetricDescriptorConstants.*;
import static com.hazelcast.internal.metrics.ProbeLevel.MANDATORY;
import static com.hazelcast.internal.util.Preconditions.checkNotNull;

/**
 * A Metric pack for exposing {@link java.lang.Runtime} metrics.
 */
public final class RuntimeMetricSet {

    private RuntimeMetricSet() {
    }

    /**
     * Registers all the metrics in this metrics pack.
     *
     * @param metricsRegistry the MetricsRegistry upon which the metrics are registered.
     */
    public static void register(MetricsRegistry metricsRegistry) {
        checkNotNull(metricsRegistry, "metricsRegistry");

        Runtime runtime = Runtime.getRuntime();
        RuntimeMXBean mxBean = ManagementFactory.getRuntimeMXBean();

        metricsRegistry.registerStaticProbe(runtime, RUNTIME_FULL_METRIC_FREE_MEMORY, MANDATORY, Runtime::freeMemory);
        metricsRegistry.registerStaticProbe(runtime, RUNTIME_FULL_METRIC_TOTAL_MEMORY, MANDATORY, Runtime::totalMemory);
        metricsRegistry.registerStaticProbe(runtime, RUNTIME_FULL_METRIC_MAX_MEMORY, MANDATORY, Runtime::maxMemory);
        metricsRegistry.registerStaticProbe(runtime, RUNTIME_FULL_METRIC_USED_MEMORY, MANDATORY, JVMUtil::usedMemory);
        metricsRegistry.registerStaticProbe(runtime, RUNTIME_FULL_METRIC_AVAILABLE_PROCESSORS, MANDATORY, Runtime::availableProcessors);
        metricsRegistry.registerStaticProbe(mxBean, RUNTIME_FULL_METRIC_UPTIME, MANDATORY, RuntimeMXBean::getUptime);
    }
}
